package com.andy.aspect;

import org.aopalliance.intercept.Joinpoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LogAspect {


    @Pointcut("execution(* com.andy.service.*.*(..))")
    public void pointcut(){}

    @Around("pointcut()")
    public Object logAdvice(ProceedingJoinPoint jp) throws Throwable {
        Object[] args = jp.getArgs();
        String name = jp.getSignature().getName();
        Object result = null;
        long threadId = Thread.currentThread().getId();

        System.out.println("method "+name+" begin "+ threadId);
        try {
            result = jp.proceed(args);
        }catch (Throwable e){
            System.out.println("method "+name+" error "+threadId);
            throw e;
        }finally {
            System.out.println("method "+name+" end "+threadId);
        }

        return result;
    }

}
